package us.yesor.atlast;

import us.yesor.atlast.experimental.SampleDataLoader;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;
import android.util.Log;
import android.view.View;
import android.widget.ListView;

public class ViewCollectionsActivity extends ListActivity {

	private SimpleCursorAdapter adapter;
	private String activeCollection;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.list);
		
		//TODO remove this before production
		//SampleDataLoader.loadSampleData(this);
		
		SharedPreferences sp = getSharedPreferences("prefs", Context.MODE_PRIVATE);
		activeCollection = sp.getString("activeCollection", "No Collection Chosen");
		
		Cursor c = getContentResolver().query(Uri.parse(InteractionTable.CONTENT_URI+"/collections"), null,
				null, null, null);
		startManagingCursor(c);
		
		String[] columns = new String[] {c.getColumnName(1),c.getColumnName(2),c.getColumnName(3)};
		
		int[] to = new int[] {R.id.collection_entry, R.id.count_entry, R.id.time_entry};
		
		//TODO deprecated SimpleCursorAdapter
		adapter = new SimpleCursorAdapter(this, R.layout.collections_entry, c, columns, to);
		
		this.setListAdapter(adapter);
	}
	
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		Cursor c = (Cursor) adapter.getItem(position);
		SharedPreferences sp = getSharedPreferences("prefs", Context.MODE_PRIVATE);
		Editor e = sp.edit();
		e.putString("activeCollection", c.getString(1));
		e.commit();
		Intent myIntent = new Intent(this, ViewItemsActivity.class);
		startActivity(myIntent);
	}

}
